package com.autologistics.socket.service;

import com.autologistics.dao.OrderTableDao;
import com.autologistics.model.Ordertable;
import com.autologistics.socket.vo.OrderGetRes;

import net.sf.json.JSONObject;
/*
 * 
 * order state define
 * 1.commit
 * 2.store
 * 3.fetch
 * 4.cancel
 */


public class OrderServices {

	private JSONObject orderRequestJSON ;
	
	
	public OrderServices(JSONObject orderRequestJSON) {
		this.orderRequestJSON = orderRequestJSON;
	}

	/*
	 * test case below
	 * 0.wait delivery
	 * 1.wait fetch
	 * 2.been fetched
	 * other. not exist
	 */
	public JSONObject orderWaiteGet(){
	
		
		OrderGetRes response = new OrderGetRes();
		
		if(SpringContextWait.beanFcBeanFactory == null){
			response.setResponseCode(4);
			response.setResponseDescription("system is starting ,please wait for a moment");
			return JSONObject.fromObject(response);
		}
		
		String orderNumber = orderRequestJSON.optString("orderId");
		
		if(orderNumber == null || "".equals(orderNumber)){
			response.setResponseCode(-2);
			response.setResponseDescription("could not find parameter OrderID");
			return JSONObject.fromObject(response);
		}
		
		// query database                                                                          ordertableDao
		OrderTableDao orderTableDao = (OrderTableDao) SpringContextWait.beanFcBeanFactory.getBean("ordertableDao");
		Ordertable orderTable = orderTableDao.getByOrderID(orderNumber);
		if(orderTable == null){
			response.setResponseCode(3);
			response.setResponseDescription("order not exist");
		}else if(orderTable.getState() ==1){//match successful
			orderTable.setState(2);
			orderTableDao.updata(orderTable);
			
			response.setResponseCode(0);
			response.setResponseDescription("success");
			response.setOrderNumber(orderNumber);
		}else if(orderTable.getState() == 2){
			response.setResponseCode(4);
			response.setOrderNumber(orderTable.getOrderId());
			response.setResponseDescription("this order always set into machine");
		}else if(orderTable.getState() == 3){
			response.setResponseCode(5);
			response.setResponseDescription("this order always been fetched by end user");
			response.setOrderNumber(orderTable.getOrderId());
		}else if(orderTable.getState() == 4){
			response.setResponseCode(6);
			response.setResponseDescription("this order always been cancled");
			response.setOrderNumber(orderTable.getOrderId());
		}
		
		/*
		//interface test code
		if(orderNumber.equals("A1000000")){//match successful
			response.setOrderNumber("A1000000");
			response.setResponseCode(0);
			response.setResponseDescription("update successful");;
		}else if(orderNumber.equals("A1000001")){//order is not delivery into machine
			response.setResponseCode(4);
			response.setOrderNumber("A1000001");
			response.setResponseDescription("this order always set into machine");
		}else if(orderNumber.equals("A1000002")){
			response.setResponseCode(4);
			response.setResponseDescription("this order always been fetched by end user");
			response.setOrderNumber("A1000002");
		}else {
			response.setResponseCode(3);
			response.setResponseDescription("order not exist");
		}
		
		*/
		return JSONObject.fromObject(response);
	}
	
	public JSONObject orderGet(){//
		
		OrderGetRes response = new OrderGetRes();
		if(SpringContextWait.beanFcBeanFactory == null){
			response.setResponseCode(4);
			response.setResponseDescription("system is starting ,please wait for a moment");
			return JSONObject.fromObject(response);
		}
		String orderNumber = orderRequestJSON.optString("orderNumber");
		
		if(orderNumber == null || "".equals(orderNumber)){
			response.setResponseCode(-2);
			response.setResponseDescription("could not find parameter OrderNumber");
			return JSONObject.fromObject(response);
		}

		// query database

		// query database                                                                          
		OrderTableDao orderTableDao = (OrderTableDao) SpringContextWait.beanFcBeanFactory.getBean("ordertableDao");
		Ordertable orderTable = orderTableDao.getByOrderID(orderNumber);
		if(orderTable == null){
			response.setResponseCode(3);
			response.setResponseDescription("order not exist");
		}else if(orderTable.getState() ==1){
			response.setOrderNumber(orderNumber);
			response.setResponseCode(5);
			response.setResponseDescription("order not delivery,please manager check it");;
		}else if(orderTable.getState() == 2){//match successful
			response.setResponseCode(0);
			response.setOrderNumber(orderNumber);
			response.setResponseDescription("success");
			
			orderTable.setState(3);
			orderTableDao.updata(orderTable);
		}else if(orderTable.getState() == 3){
			response.setResponseCode(4);
			response.setResponseDescription("this order always been fetched by end user");
			response.setOrderNumber(orderTable.getOrderId());
		}else if(orderTable.getState() == 4){
			response.setResponseCode(6);
			response.setResponseDescription("this order always been cancled");
			response.setOrderNumber(orderTable.getOrderId());
		}

		/*
		//interface test code
		if(orderNumber.equals("A1000000")){//match successful
			response.setOrderNumber("A1000000");
			response.setResponseCode(5);
			response.setResponseDescription("order not delivery,please manager check it");;
		}else if(orderNumber.equals("A1000001")){//order is not delivery into machine
			response.setResponseCode(0);
			response.setOrderNumber("A1000001");
			response.setResponseDescription("successful");
		}else if(orderNumber.equals("A1000002")){
			response.setResponseCode(4);
			response.setResponseDescription("this order always been fetched by end user");
			response.setOrderNumber("A1000002");
		}else {
			response.setResponseCode(3);
			response.setResponseDescription("order not exist");
		}
		*/
		
		return JSONObject.fromObject(response);
	}
}
